package com.wise.aip.kit;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.wise.aip.auth.GatewayAuth;
import com.wise.aip.template.ParamTemplate;
import com.wise.aip.template.URLTemplate;
import com.wise.api.json.utils.ApiJsonUtils;

public class WiseDataKit {

    /**
     * 
     * <p>
     * Title: getNowBankAccountInfoByNumber
     * </p>
     * <p>
     * Description:根据编码获取目前的银行账号信息
     * </p>
     * 
     * @param gatewayHost
     * @param authorityCode
     * @param token
     * @param oldNumber
     * @param oldNumber2
     * @return Map<String, String> key: BankTypeID(接口类型码) BankTypeName(接口类型名称) FAcntNumber（银行账号）
     *         FAcntName（账户名称） FAcntBranch（开户行）
     */
    public static Map<String, String> getNowBankAccountInfoByNumber(String gatewayHost, String itemClassCode,
            String token, String oldNumber) {

        Map<String, String> map = new HashMap<String, String>(5);
        String findRes = "";
        StringBuffer log = new StringBuffer();
        log.append("**com.wise.aip.kit.WiseDataKit_getNowBankAccountInfoByNumber （根据编码获取目前的银行账号信息）*******\n");
        log.append("*****入参编码：" + oldNumber);

        try {
            findRes = find(gatewayHost, itemClassCode, token, oldNumber);
        } catch (Exception e) {
            log.append("******查询明细失败！find方法报错! \n" + e.toString());
            printLog(log.toString());
            return map;
        }

        if (StringUtils.isNotEmpty(findRes)) {
            map = ApiJsonUtils.parseJsonStrForBankAccountInfo(findRes);
        }

        printLog(log.toString());
        return map;

    }

    /**
     * 
     * <p>
     * Title: getFDPropertyByNumber
     * </p>
     * <p>
     * Description: 根据部门编码获取其部门属性信息
     * </p>
     * 
     * @param gatewayHost
     * @param itemClassCode
     * @param token
     * @param number
     * @return Map<String, String> key：FID FName
     */
    public static Map<String, String> getFDPropertyByNumber(String gatewayHost, String itemClassCode,
            String token, String number) {

        Map<String, String> map = new HashMap<String, String>(2);
        String findRes = "";
        StringBuffer log = new StringBuffer();
        log.append("*****com.wise.aip.kit.WiseDataKit_getFDPropertyByNumber （根据部门编码查询获取其现在的部门属性）*******\n");
        log.append("*****入参编码：" + number);

        try {
            findRes = find(gatewayHost, itemClassCode, token, number);
        } catch (Exception e) {
            log.append("******查询明细失败！find方法报错! \n" + e.toString());
            printLog(log.toString());
            return map;
        }

        if (StringUtils.isNotEmpty(findRes)) {
            map = ApiJsonUtils.parseJsonStrForFDProperty(findRes);
        }

        printLog(log.toString());
        return map;

    }

    /**
     * 
     * <p>
     * Title: getNowNumberByCusField
     * </p>
     * <p>
     * Description: 根据自定义字段过滤查询获取其现在的编码
     * </p>
     * 
     * @param gatewayHost 网关地址 eg:http://218.69.112.158:8000/K3API
     * @param itemClassCode 基础资料类型码
     * @param token 令牌
     * @param cusField 自定义字段名
     * @param filterVal 过滤值
     * @return
     */
    public static String getNowNumberByCusField(String gatewayHost, String itemClassCode, String token,
            String filter) {

        String filterRes = "";
        String nowNumber = "";
        StringBuffer log = new StringBuffer();
        log.append("*****com.wise.aip.kit.WiseDataKit_getNowNumberByCusField （根据自定义字段过滤查询获取其现在的编码）*******\n");
        log.append("*****入参查询列表过滤内容：" + filter);

        try {
            filterRes = filter(gatewayHost, itemClassCode, token, filter);
        } catch (Exception e) {
            log.append("******查询列表过滤失败！filter方法报错! \n" + e.toString());
            printLog(log.toString());
            return nowNumber;
        }

        if (!StringUtils.isEmpty(filterRes)) {
            nowNumber = ApiJsonUtils.parseJsonStrForNumber(filterRes);
            log.append("\n*********获取到的基础资料编码：" + nowNumber);
        }

        printLog(log.toString());
        return nowNumber;

    }

    /**
     * 
     * <p>
     * Title: getAccountGroupIDByNumber
     * </p>
     * <p>
     * Description: 根据编码查询科目明细获取其现在的科目类别代码
     * </p>
     * 
     * @param gatewayHost 网关地址 eg:http://218.69.112.158:8000/K3API
     * @param itemClassCode 基础资料类型码
     * @param token 令牌
     * @param number 科目编码
     * @return
     */
    public static Integer getAccountGroupIDByNumber(String gatewayHost, String itemClassCode, String token,
            String number) {

        Integer groupID = null;
        String detailRes = "";
        StringBuffer log = new StringBuffer();
        log.append("***com.wise.aip.kit.WiseDataKit_getAccountGroupIDByNumber （根据编码查询科目明细获取其现在的科目类别代码）***\n");
        log.append("*****入参编码：" + number);

        try {
            detailRes = find(gatewayHost, itemClassCode, token, number);
        } catch (Exception e) {
            log.append("******查询明细报错! \n" + e.toString());
            printLog(log.toString());
            return groupID;
        }

        if (!StringUtils.isEmpty(detailRes)) {
            groupID = ApiJsonUtils.parseJsonStrForGroupID(detailRes);
            log.append("\n*********获取到的科目类别代码：" + groupID);
        }

        printLog(log.toString());
        return groupID;

    }

    /**
     * 
     * <p>
     * Title: filter
     * </p>
     * <p>
     * Description: 查询列表过滤
     * </p>
     * 
     * @param gatewayHost
     * @param itemClassCode
     * @param token
     * @param filter 过滤条件
     * @return
     * @throws Exception
     */
    private static String filter(String gatewayHost, String itemClassCode, String token, String filter)
            throws Exception {
        String requestURL = URLTemplate.getList(gatewayHost, itemClassCode, token);
        String param = ParamTemplate.listByFilter(filter);
        return GatewayAuth.gatewayPostJson(requestURL, param);
    }

    /**
     * 
     * <p>
     * Title: find
     * </p>
     * <p>
     * Description: 查询明细
     * </p>
     * 
     * @param gatewayHost
     * @param itemClassCode
     * @param token
     * @param number
     * @return
     * @throws Exception
     */
    private static String find(String gatewayHost, String itemClassCode, String token, String number)
            throws Exception {
        return GatewayAuth.gatewayPostJson(URLTemplate.getDetail(gatewayHost, itemClassCode, token),
                ParamTemplate.getDetail(number));
    }

    private static void printLog(String log) {
        System.out.println(log);
    }

}
